// pages/home/home.js
import {
  HTTP
} from '../../utils/http'
let http = new HTTP()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moreShow: false,
    workDetail: {},
    workId: '',
    userInfo: {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      workId: options.id
    })
    this.getWorkDetail();
  },

  getWorkDetail() {
    http.request({
      url: '/ai/draw/picture/detail',
      data: {
        id: this.data.workId
      },
      success: (res) => {
        if (res.code === "0") {
          this.setData({
            workDetail: res.data,
          })
        }
      }
    })
  },

  /**
   * 收藏
   */
  collectTap() {
    if (this.data.workDetail.isCollect === 0) {
      http.request({
        url: '/ai/draw/picture/collect',
        method: 'post',
        data: {
          picId: this.data.workId
        },
        success: (res) => {
          if (res.code === "0") {
            this.getWorkDetail();
          }
        }
      })
    } else {
      http.request({
        url: '/ai/draw/picture/cancel/collect',
        data: {
          picId: this.data.workId
        },
        success: (res) => {
          if (res.code === "0") {
            this.getWorkDetail();
          }
        }
      })
    }

  },

  /**
   * 点赞
   */
  starTap() {
    if (this.data.workDetail.isStar === 0) {
      http.request({
        url: '/ai/draw/picture/star',
        data: {
          picId: this.data.workId
        },
        success: (res) => {
          if (res.code === "0") {
            this.getWorkDetail();
          }
        }
      })
    } else {
      http.request({
        url: '/ai/draw/picture/cancel/star',
        data: {
          picId: this.data.workId
        },
        success: (res) => {
          if (res.code === "0") {
            this.getWorkDetail();
          }
        }
      })
    }

  },

  creation() {
    // todo 暂未找到boxId 拼接到参数后面
    wx.redirectTo({
      url: '/pages/box-detail/box-detail?id=' + this.data.workDetail.boxId,
    })
  },

  /**
   * 关注
   */
  focusTap() {
    http.request({
      url: '/ai/draw/account/focus/account',
      data: {
        focusedAccountId: this.data.workDetail.accountInfo.id
      },
      success: (res) => {
        if (res.code === "0") {
          this.getWorkDetail();
        }
      }
    })
  },

  cancelFocusTap() {
    http.request({
      url: '/ai/draw/account/cancel/focus/account',
      data: {
        focusedAccountId: this.data.workDetail.accountInfo.id
      },
      success: (res) => {
        if (res.code === "0") {
          this.getWorkDetail();
        }
      }
    })
  },
  onMore() {
    this.setData({
      moreShow: true
    })
  },
  onMoreClose() {
    this.setData({
      moreShow: false
    });
  },
  /**
   * 举报
   */
  onReport() {
    http.request({
      url: '/ai/draw/report/add',
      data: {
        relationId: this.data.workId,
        type: 0
      },
      success: (res) => {
        if (res.code === "0") {

        }
      }
    })
  },
  onDownFile() {
    let url = this.data.workDetail.url
    let that = this
    //若二维码未加载完毕，加个动画提高用户体验
    wx.showToast({
     icon: 'loading',
     title: '正在保存图片',
     duration: 1000
    })
    //判断用户是否授权"保存到相册"
    wx.getSetting({
     success (res) {
      //没有权限，发起授权
      if (!res.authSetting['scope.writePhotosAlbum']) {
       wx.authorize({
        scope: 'scope.writePhotosAlbum',
        success () {//用户允许授权，保存图片到相册
         that.savePhoto(url);
        },
        fail () {//用户点击拒绝授权，跳转到设置页，引导用户授权
         wx.openSetting({
          success () {
           wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
             that.savePhoto(url);
            }
           })
          }
         })
        }
       })
      } else {//用户已授权，保存到相册
       that.savePhoto(url)
      }
     }
    })
  },
//保存图片到相册，提示保存成功
 savePhoto(url) {
  let that = this
  wx.downloadFile({
   url: url,
   success: function (res) {
    wx.saveImageToPhotosAlbum({
     filePath: res.tempFilePath,
     success(res) {
      wx.showToast({
       title: '保存成功',
       icon: "success",
       duration: 1000
      })
     }
    })
   }
  })
 },



  onIsPrivate(e) {
    http.request({
      url: '/ai/draw/picture/update',
      method: "POST",
      data: {
        id: this.data.workId,
        isPrivate: e.currentTarget.dataset.value
      },
      success: (res) => {
        this.getWorkDetail()
      }
    })
  },
  /**
   * 跳转用户详情
   */
  jumpUser(e) {
    let userId = e.currentTarget.dataset.userid
    wx.navigateTo({
      url: '/pages/user/user?userId=' + userId,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this
    wx.getStorage({
      key: 'userId',
      success: function (res) {
        _this.setData({
          "userInfo.id": res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(event) {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})